package com.wuxiaofan.auth.config;

import com.wuxiaofan.auth.entity.OauthClientDetails;
import com.wuxiaofan.auth.service.OauthClientDetailsService;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Component;

/**
 * @author wuxiaofan
 * @date 2021/11/15 10:07 上午
 */
@Component
@Slf4j
public class CustomClientDetailsService implements ClientDetailsService {

    @Resource
    private OauthClientDetailsService oauthClientDetailsServicel;

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        OauthClientDetails oauthClientDetails = oauthClientDetailsServicel.findByClientId(clientId);
        BaseClientDetails clientDetails = new BaseClientDetails();
        clientDetails.setClientId(oauthClientDetails.getClientId());
        clientDetails.setClientSecret(oauthClientDetails.getClientSecret());
        List<String> sopes = new ArrayList<>();
        sopes.add("app");
        clientDetails.setScope(sopes);

        List<String> authorizedGrantTypes = new ArrayList<>();
        authorizedGrantTypes.add("authorization_code");
        clientDetails.setAuthorizedGrantTypes(authorizedGrantTypes);

        Set<String> registeredRedirectUris = new HashSet<>();
        registeredRedirectUris.add("http://localhost:9086/auth/auth/code");
        clientDetails.setRegisteredRedirectUri(registeredRedirectUris);
        return clientDetails;
    }
}
